<?php

namespace app\admin\controller\paotui;

use app\common\controller\Backend;

/**
 * 路程价格管理
 *
 * @icon fa fa-circle-o
 */
class Distanceprice extends Backend
{
    
    /**
     * Distanceprice模型对象
     * @var \app\admin\model\paotui\Distanceprice
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\paotui\Distanceprice;

    }

    public function index()
    {
        if($this->request->isPost()){
            $params = $this->request->post("row/a");
            if (isset($params['general'])){
                $type = 1;
                $value = $params['general'];
            }
            $this->model->where(['type'=>$type])->delete();
            foreach ($value as $v){
                $model = new \app\admin\model\paotui\Distanceprice();
                $model->save(['type'=>$type,
                    'min'=>$v['min'],
                    'max'=>$v['max'],
                    'step'=>$v['step'],
                    'price'=>$v['price']]);
            }
            $this->success("请求成功");
            exit;
        }
        $model = new \app\admin\model\paotui\Distanceprice();
        $generalList = $model->where(['type'=>"1"])->select();
        $general = [];
        foreach ($generalList as $v){
            $general[] = ['min'=>$v['min'],'max'=>$v['max'],'step'=>$v['step'],'price'=>$v['price']];
        }
        $this->assign('general',json_encode($general));

        return $this->fetch();
    }
}
